import {
	getlogin,
	poslogin,
	getpettype,
	getbeed,
	getsex,
	getyn,
} from "@/pages/api/apihost.js"
export default {
	data() {
		return {
			filelist: [],
			isauth: false,
		}
	},
	methods: {
		getmixlist(){
			
		},
		getType() {
			if(uni.getStorageSync("token")){
				getpettype().then(r => {
					uni.setStorageSync('pettype', JSON.stringify(r))
				}),
				getbeed().then(r => {
					uni.setStorageSync('beed', JSON.stringify(r))
				}),
				getsex().then(r => {
					uni.setStorageSync('sex', JSON.stringify(r))
				})
			}
		},
		setuser() {
			let id = setTimeout(() => {
				console.log(this.userinfo, '345')
				this.usrimg = this.userinfo.userInfo.avatarUrl
				this.nickName = this.userinfo.userInfo.nickName
				clearTimeout(id)
			}, 1000)
		},
		async login() {
			let t = this
			await uni.getUserProfile({
				desc: '用于用户登录',
				success(usr) {
					t.userinfo = usr;
					console.log(usr, 'user')
				},
				fail(e) {
					t.showtost('授权失败,请稍后再试')
				}
			})
			await wx.login({
				success: (r) => {
					getlogin({
						code: r.code
					}).then(r => {
						if (r.isRegisterUser) {
							t.setuser()
							t.onclose()
							t.showtost('授权成功')
							uni.setStorageSync('token', r.loginToken);
							t.getType()
							t.getmixlist()
						} else {
							t.faileLogin(r)
						}

					})
				}
			})
		},
		faileLogin(r) {
			let id = setTimeout(() => {
				console.log(this.userinfo, 'data1234')
				poslogin({
					openId: r.openid,
					nickName: this.userinfo.userInfo.nickName,
					avatarUrl: this.userinfo.userInfo.avatarUrl
				}).then(data => {
					this.onclose()
					this.showtost('授权成功')
					uni.setStorageSync('token', data);
					this.getType()
					this.getmixlist()
				})
				clearTimeout(id)
			}, 500)
		},
		onclose() {
			uni.showTabBar()
			let id = setTimeout(() => {
				this.isauth = false
				clearTimeout(id)
			})
		},
		setauth() {
			uni.hideTabBar()
			this.isauth = true
		},
		setstore(name, value) {
			uni.setStorageSync(name, value)
		},
		getstore(name) {
			return uni.getStorageSync(name)
		},
		showtost(v) {
			this.$refs.uToast.show({
				message: `${v}`
			})
		},
		getpettypetext(id) {
			let arr = JSON.parse(uni.getStorageSync('pettype'))
			let name = ''
			arr.forEeach(v => {
				if (v.dictValue == id) {
					name = v.dictLabel
				}
			})
			return name
		},
		getbeed(id) {
			let arr = JSON.parse(uni.getStorageSync('beed'))
			let name = ''
			arr.forEeach(v => {
				if (v.dictValue == id) {
					name = v.dictLabel
				}
			})
			return name
		},
		async afterRead(event) {
			// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file);
			let fileListLen = this.filelist.length;
			// 图片类型限制

			lists.map((item) => {
				this.filelist.push({
					...item,
					status: 'uploading',
					message: '上传中',
				});
			});
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url, lists[i]);
				let item = this.filelist[fileListLen];
				this.filelist.splice(fileListLen, 1, {
					...item,
					status: 'success',
					message: '',
					url: JSON.parse(result).url,
				});
				fileListLen++;
			}
		},
		deletePic(event){
		  this.filelist.splice(event.index, 1);
		},
		uploadFilePromise(url, item) {
			console.log(item, 'item12345')
			// let nsuffix = url.match(/[^\.]\w*$/)[0]

			return new Promise((resolve, reject) => {

				let token = uni.getStorageSync("token")
				let a = uni.uploadFile({
					url: 'http://121.41.48.194/prod-api/common/uploadOSS', // 仅为示例，非真实的接口地址
					filePath: url,
					name: 'file',
					header: {
						Authorization: `Bearer ${token}`,
					},
					success: (res) => {
						setTimeout(() => {
							console.log(res.data, 'resssss')
							resolve(res.data);
						}, 1000);
					},
				});
			});
		}

	}
}